<?php
/**
 * 用户安全key
*/
apf_require_class("Bll_BLL");
apf_require_class('Bll_Hr_OutInterface');
class Bll_Flow_UserSecurityKey extends BLL {

    /**
     * 设置用户的安全密文
     * @param int $uid
     * @param string $key
     * @return boolean
     */
    public function set_user_miwen($user_info,$key) {
        $result = false;
        if(empty($user_info['user_id']) || !is_numeric($user_info['user_id']) || empty($key) || empty($user_info['user_name']) || empty($user_info['user_code'])) {
            return $result;
        }
        $rs = $this->_get_default_dao()->find_by_id($user_info['user_id']);

        $time = time();
        if(empty($rs)) {
            $result = $this->_get_default_dao()->insert(array('user_id' => $user_info['user_id'],'user_name' => $user_info['user_name'],'user_code' => $user_info['user_code'],'security_key' => $key,'created'=>$time,'last_update'=>$time));
        }else {
            $result = $this->_get_default_dao()->update(array('security_key' => $key,'last_update'=>$time),
                      array('user_id' => $user_info['user_id']));
            $result = $this->_get_default_dao()->update(array('security_key' => $key,'user_name' => $user_info['user_name'],'user_code' => $user_info['user_code'],'last_update'=>$time),
                      array('user_id' => $user_info['user_id']));
        }
        $result = $result ? true:false;
        return $result;
    }

    public function get_user_miwen($user_id) {
        return $this->_get_default_dao()->find_by_id($user_id);
    }

    /**
     * 获得系统密文
     * @return string
     */
    public function get_system_key() {
      return empty($_SESSION['system_sa']) ? '0':$_SESSION['system_sa'];
    }

    /**
     * 检查是否有数据，以做为是否做了初始化的动作
     * @return bool
     */
    public function check_init_enable() {
        $rs = $this->_get_default_dao()->find(array(), $order = null, $limit = 1, $offset = 0);
        return $rs ? false:true;
    }

    /**
     * 分页查询密文数据
     * @param array $params = array('user_name','user_code','s_time','e_time','order','page_size','page_num')
     * @return array
     */
    public function search_user_list($params = array()) {
        $order = empty($params['order']) ? "last_update desc":$params['order'];
        $limit = empty($params['page_size']) ? "10":$params['page_size'];
        $page_num = (empty($params['page_num']) || !is_numeric($params['page_num'])) ? 1:$params['page_num'];
        $offset = ($page_num-1)*$limit;

        $where = array();
        if(!empty($params['user_name'])) {
            $where["user_name like '{$params['user_name']}%' and 1 = ?"] = 1;
        }
        if(!empty($params['user_code'])) {
            $where["user_code like '{$params['user_code']}%' and 1 = ?"] = 1;
        }
        if(isset($params['s_time'])) {
            $where["last_update >= {$params['s_time']} and 1 = ?"] = 1;
        }
        if(isset($params['e_time'])) {
            $where["last_update <= {$params['e_time']} and 1 = ?"] = 1;
        }

        $list = $this->_get_default_dao()->find($where, $order, $limit, $offset);
        $count = $this->_get_default_dao()->find_count($where);
        return array('list'=>$list,'total'=>$count);
    }

    /**
     * 员工安全key
     * @return _get_dao_flow_usersecuritykey
     */
    private function _get_default_dao(){
        return $this->get_dao('Dao_Flow_SecurityKey');
    }

}